<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false"%>
<!-- 	导入Struts 2标签库 -->
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>index</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <style>

        #box1 {
            margin-left: 25px;
        }
    </style>
</head>
<body>
<blockquote class="layui-elem-quote">为节省服务器开销，以下示例均未配置真实上传接口，所以每次上传都会报提示：请求上传接口出现异常，这属于正常现象。</blockquote>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>常规使用：普通图片上传</legend>
</fieldset>
<div id="box1">
    <div class="layui-upload">
        <button type="button" class="layui-btn" id="test1">上传图片</button>
        <div class="layui-upload-list">
            <img class="layui-upload-img" id="demo1" style="width: 92px;height: 92px;">
            <p id="demoText"></p>
        </div>
    </div>
</div>
<script type="text/javascript" src="layui/layui.all.js"></script>
<script>
    layui.use(['table','form', 'layer','laydate', 'jquery', 'upload'], function (table,form, layer,laydate, $,upload) {
        var $ = layui.jquery, table = layui.table, form = layui.form, upload = layui.upload;

        var uploadInst = upload.render({
            elem: '#test1'
            ,url: 'upload.action'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                location.href = 'toPicList.action';
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });

    });
</script>
</body>
</html>
